package rpc

import (
	"context"
	"fmt"

	"bmember/internal/service"

	"google.golang.org/grpc"

	pb "bmember/api/pb/server"
)

type mRpchandler struct {
	*RpcBase
	name string
	pb.UnimplementedMemberServer
}

// NewMemberRpcHandler 初始化
func NewMemberRpcHandler() *mRpchandler {
	return &mRpchandler{
		RpcBase:                   base,
		name:                      "rpc_member_handler",
		UnimplementedMemberServer: pb.UnimplementedMemberServer{},
	}
}

// RegisterHandler 注册服务
func (m *mRpchandler) RegisterHandler(server *grpc.Server) {
	pb.RegisterMemberServer(server, m)
}

// SayHello 测试
func (m *mRpchandler) SayHello(_ context.Context, req *pb.HelloReq) (*pb.HelloReply, error) {
	if req == nil {
		return nil, nil
	}
	msg := fmt.Sprintf("Hello %s; id: %d", req.Name, req.Id)
	return &pb.HelloReply{
		Message: msg,
	}, nil
}

// GetMemberOpenIdOfBilinApp 根据id，渠道获取用户 openid
func (m *mRpchandler) GetMemberOpenIdOfBilinApp(ctx context.Context, req *pb.MemberOpenIdGetReq) (*pb.MemberOpenIdGetReply, error) {
	if req == nil {
		return nil, nil
	}
	return service.GetMemberSrv().GetMemberOpenIdByMemberId(ctx, req.MemberId, req.BilinApp)
}

// SearchMembersMobileNickname 用户信息搜索
func (m *mRpchandler) SearchMembersMobileNickname(ctx context.Context, req *pb.MemberMobileNicknameSearchReq) (*pb.MemberMobileNicknameListReply, error) {
	if req == nil {
		return nil, nil
	}
	return service.GetMemberSrv().SearchMembersMobileNickname(ctx, req.BrandId, req.Mobile, req.Nickname)
}

// GetMembersMobileNickname 用户查询 by ids
func (m *mRpchandler) GetMembersMobileNickname(ctx context.Context, req *pb.MemberMobileNicknameGetReq) (*pb.MemberMobileNicknameListReply, error) {
	if req == nil {
		return nil, nil
	}
	return service.GetMemberSrv().GetMembersMobileNickname(ctx, req.MemberIds)
}

// GetRelatedMemberIds 根据会员id换取与之关联的会员id，由会员合并引入
func (m *mRpchandler) GetRelatedMemberIds(ctx context.Context, req *pb.GetRelatedMemberIdsReq) (*pb.GetRelatedMemberIdsReply, error) {
	if req == nil {
		return nil, nil
	}
	return service.GetMemberSrv().GetMemberRelatedIds(ctx, req.BrandId, req.MemberId)
}
